﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace sands
{
    partial class frmAdd : SandsForm
    {
        private String oldID = String.Empty;

         public String OldID {
             get
             {
                 return oldID;
             }
             set
             {
                 oldID = value;
             }
         }

        public frmAdd()
        {
            InitializeComponent();
        }

        private void cbTuition_CheckedChanged(object sender, EventArgs e)
        {
            ctlTuition.Enabled = cbTuition.Checked;
            groupBox2.Enabled = !cbTuition.Checked;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            DataBase db = DataBase.GetInstance();

            if(ctlIndex.Text != OldID)
            {
                if(!db.isUnique(ctlIndex.Text))
                {
                    MessageBox.Show("Niestey taki indeks już istnieje.", "Błąd: zmiana indeksu", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    DialogResult = DialogResult.None;
                }
                else if ( ! String.IsNullOrEmpty(oldID) && db.GetDeposits(db.GetStudent(oldID)).Length > 0)
                {
                    if (DialogResult.Yes == MessageBox.Show("Czy chcesz zachować historię wpłat tego ucznia?", "Zmiana indeksu", MessageBoxButtons.YesNo, MessageBoxIcon.Question))
                    {
                        db.ChangeDepositID(oldID, ctlIndex.Text);
                    }
                    else
                    {
                        db.RemoveDeposits(oldID);
                    }
                }
            }



            if (!StudentDetails.CheckIfValid(this.btnOK.Text.Equals("Zmień") ? true : false))
                DialogResult=DialogResult.None;
        }

        private void frmAdd_Load(object sender, EventArgs e)
        {
            groupBox3.DataBindings.Add("Enabled", cbRate, "Checked");
            ctlIndex.BackColor = Color.White;
        }

        private void ctlIndex_TextChanged(object sender, EventArgs e)
        {
            DataBase db = DataBase.GetInstance();
            if (ctlIndex.Text != OldID && !db.isUnique(ctlIndex.Text))
            {
                ctlIndex.BackColor = Color.Red;
            }
            else
                ctlIndex.BackColor = Color.LawnGreen;
        }
    }
}
